
#################################################################################################

#  Description: Generating Figures 1 and 2 from Fleming, Thomas G. (Forthcoming) 'Parliamentary 
#               Procedure under Theresa May: Nothing has Changed?', Parliamentary Affairs.
#  Author:      Thomas G. Fleming
#  Date:        30/05/2020
#  Note:        Requires file "data.csv" to be saved in same folder

#################################################################################################

rm(list=ls())

library(ggplot2)

library(rstudioapi)                               # sets wd as current folder
current_path <- getActiveDocumentContext()$path
setwd(dirname(current_path))

#####################################################
# Loading data and calculating additional variables #
#####################################################

  # loads data on changes to Standing Orders under each post-war PM

  data <- read.csv("./data.csv")
 
  # calculates tenure (in years) of each PM

  data$start.date <- as.Date(data$start.date, format = "%Y-%m-%d")
  data$end.date <- as.Date(data$end.date, format = "%Y-%m-%d")

  data$pm.tenure <- (data$end.date - data$start.date) / 365

  # calculates the total number of subarticles added, deleted, or changed under each PM as a share of the starting number
  
  data$percentage.changed <- 
    ((data$subarticles.added + data$subarticles.deleted + data$subarticles.changed) / data$starting.subarticles.total)*100

  # weights this by PM's tenure
  
  data$percentage.changed.annual <- data$percentage.changed/ as.numeric(data$pm.tenure)
  
####################################
# Creating axis labels for figures #
####################################
  
# Creating axis labels
  
  PM.labels <- c("May (Con) \n(2016-2019)", "Cameron (Con)\n(2015-2016)", "Cameron (Coalition)\n(2010-2015)", 
                 "Brown (Lab)\n(2007-2010)", "Blair (Lab)\n(1997-2007)", "Major (Con)\n(1990-1997)", "Thatcher (Con)\n(1979-1990)", 
                 "Callaghan (Lab)\n(1976-1979)", "Wilson (Lab)\n(1974-1976)", "Heath (Con)\n(1970-1974)", 
                 "Wilson (Lab)\n(1964-1970)", "Douglas-Home (Con)\n(1963-1964)", "Macmillan (Con)\n(1957-1963)", 
                 "Eden (Con)\n(1955-1957)", "Churchill (Con)\n(1951-1955)", "Attlee (Lab)\n(1945-1951)")
  
###################################################################################################################
# Figure 1                                                                                                        #
#  The number of subarticles added, deleted, or changed under each PM, as a % of the initial subarticles          #
###################################################################################################################
  
  ggplot(data = data, aes(x = PM, y = percentage.changed))+
    geom_bar(stat = "identity", width = 0.5, fill = "black")+
    coord_flip()+
    theme_classic()+
    scale_x_discrete(limits=c("May", "Cameron (Con)", "Cameron (Coal)", "Brown", "Blair", "Major", 
                              "Thatcher", "Callaghan", "Wilson II", "Heath", "Wilson I",
                              "Douglas-Home", "Macmillan", "Eden", "Churchill", "Attlee"),
                     labels = PM.labels)+
    labs(x = "", y = "\n% change to Standing Orders")+
    theme(axis.title.x = element_text(face = "bold"),
          axis.title.y = element_text(face = "bold"),
          panel.border = element_rect(colour = "black", fill = NA, size = 0.7), # adds rectangle
          axis.line = element_blank())                                          # removes axis lines because they reinforce it
  
  ggsave("./figure1.png", width = 5.5, height = 7, units = "in")
  
####################################################################################################################################
# Figure 2                                                                                                                         #
#  The number of subarticles added, deleted, or changed under each PM, as a % of the initial subarticles,  weighted by PMs' tenure #
####################################################################################################################################
  
  ggplot(data = data, aes(x = PM, y = percentage.changed.annual))+
    geom_bar(stat = "identity", width = 0.5, fill = "black")+
    coord_flip()+
    theme_classic()+
    scale_x_discrete(limits=c("May", "Cameron (Con)", "Cameron (Coal)", "Brown", "Blair", "Major", 
                              "Thatcher", "Callaghan", "Wilson II", "Heath", "Wilson I",
                              "Douglas-Home", "Macmillan", "Eden", "Churchill", "Attlee"),
                     labels = PM.labels)+
    labs(x = "", y = "\n% change to Standing Orders (per year)")+
    theme(axis.title.x = element_text(face = "bold"),
          axis.title.y = element_text(face = "bold"),
          panel.border = element_rect(colour = "black", fill = NA, size = 0.7), # adds rectangle
          axis.line = element_blank())                                          # removes axis lines because they reinforce it
  
  ggsave("./figure2.png", width = 5.5, height = 7, units = "in")
  
##########################################################################################
  
rm(list = ls())
